<?php
namespace api\common\model;
use think\Db;
class Staff extends \think\Model 
{
    protected $autoWriteTimestamp = true;
    protected $table = 'staff';
    // token,会员编号,昵称,性别,头像,状态,用户地址,手机号,金额,vip开始时间,vip结束时间
    protected $login = array('userId','token','userNumber','userName','userPhone','userAmount','userPic','userSex','type','status');

    /**
     * 员工授权登录
     */
    public function xcx_login($openid,$unionid,$userName,$userSex,$userPic){
        $res = false;
        //先根据opendid和unionid查询用户
        $queue_sel = $this->selUserByou($openid,$unionid);
        if($queue_sel){//存在用户就直接返回
            $res = $queue_sel;
        }else{//不存在就先添加再返回
            $data['openId'] = $openid;
            $data['unionId'] = $unionid;
            $data['userName'] = $userName;
            $data['userSex'] = $userSex;
            $data['userPic'] = $userPic;

            // 设置默认数据
            $date = date('Y-m-d H:i:s');
            $number = random(4,1);
            $data['userNumber'] = $number;
            $data['userStatus'] = 1;
            $data['addtime'] = $date;
            $data['updateTime'] = $date;
            $data['token'] = GenerateToken().$number;

            $insert = Db::name($this->table)->insert($data);
            if($insert){
                $queue_j_sel = $this->selUserByou($openid,$unionid);
                if($queue_j_sel){
                    $res = $queue_j_sel;
                }
            }
        }
        return $res;
    }

    /**
     * 根据opendid 和 unionid查询员工
     */
    public function selUserByou($openid,$unionid){
        $queue_sel = Db::name($this->table)->field($this->login)->where('openId',$openid)->where('unionId',$unionid)->find();
        return $queue_sel;
    }


    /**
     * 员工账户密码登录
     */
    public function accountLogin($account,$pwd){
        $sel = Db::name($this->table)->field('userId')->where('userName',$account)->where('userPass',$pwd)->find();
        $date = getNowDate();
        $data['updtime'] =$date;
        $data['token'] = GenerateToken(2).$sel['userId'].'';
        $res = false;
        $upd = Db::name($this->table)->where('userId',$sel['userId'])->update($data);
            if($upd){
                $queue_j_sel = $this->selStaff($account);
                if($queue_j_sel){
                    $res = $queue_j_sel;
                }
            }
        // 生成token
        return $res;
    }

    /**
     * 根据账户查看是否有员工
     */
    public function selStaff($account){
        $sel = Db::name($this->table)->field($this->login)->where('userName',$account)->find();
        return $sel;
    }

    /**
     * 根据员工id获取信息
     * @param $staffId 
     * @return $data
     */
    public function selById($staffId){
        $sel = Db::name($this->table)->find($staffId);
        return $sel;
    }

    /**
     * 根据token获取员工信息
     */
    public function selByToken($token){
        $sel = Db::name($this->table)->where('token',$token)->find();
        return $sel;
    }
}
